package activities;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.BasicStroke;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import orm.Exercise;
import orm.Set;

import com.example.intellifit.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification.Style;
import android.graphics.Color;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class ExerciseStatisticActivity extends Activity {

	// Demo data sets for the graphs
	private XYMultipleSeriesDataset mDataset2;
	private XYMultipleSeriesRenderer mRenderer2;
	private GraphicalView mChartView2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exercise_satistic);
		
		ArrayList<Exercise> ex = createDummyExercise();
		mDataset2 = getDemoDataset2(ex);
		mRenderer2 = getDemoRenderer2(ex);
		
		LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
		mChartView2 = ChartFactory.getLineChartView(this, mDataset2,
				mRenderer2);

		layout.addView(mChartView2, new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.exercise_satistic, menu);
		
		return true;
	}
	
	/**
	 * 
	 */
	private ArrayList<Exercise> createDummyExercise(){
		ArrayList<Exercise> exerciseList = new ArrayList<Exercise>();

		
		Date date = new Date();
		Date time = new Date();
		SimpleDateFormat iso8601FormatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat iso8601FormatTime = new SimpleDateFormat("HH:mm:ss");
		
		String ds = "2012-05-20 20:15:11";
		String ts = "00:01:11";
		try {
			date = iso8601FormatDate.parse(ds);
			time = iso8601FormatTime.parse(ts);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Exercise ex = new Exercise(0, 1, "Push-ups", date);
		ArrayList<Set> setList = new ArrayList<Set>();
		setList.add(new Set(0, 1, 0, time, 0, 1));
		setList.add(new Set(0, 1, 12, time, 20, 1));
		setList.add(new Set(0, 1, 10, time, 22, 1));
		setList.add(new Set(0, 1, 8, time, 23, 1));
		ex.setSetList(setList);
		exerciseList.add(ex);
		
		
		ds = "2012-05-25 20:15:11";
		ts = "00:01:11";
		try {
			date = iso8601FormatDate.parse(ds);
			time = iso8601FormatTime.parse(ts);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ex = new Exercise(0, 1, "Push-ups", date);
		setList = new ArrayList<Set>();
		setList.add(new Set(0, 1, 30, time, 10, 1));
		setList.add(new Set(0, 1, 12, time, 24, 1));
		setList.add(new Set(0, 1, 14, time, 25, 1));
		setList.add(new Set(0, 1, 18, time, 15, 1));
		ex.setSetList(setList);
		exerciseList.add(ex);
		
	
		ds = "2012-05-28 20:15:11";
		ts = "00:01:11";
		try {
			date = iso8601FormatDate.parse(ds);
			time = iso8601FormatTime.parse(ts);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ex = new Exercise(0, 1, "Push-ups", date);
		setList = new ArrayList<Set>();
		setList.add(new Set(0, 1, 20, time, 15, 1));
		setList.add(new Set(0, 1, 12, time, 19, 1));
		setList.add(new Set(0, 1, 16, time, 24, 1));
		setList.add(new Set(0, 1, 18, time, 25, 1));
		ex.setSetList(setList);
		exerciseList.add(ex);
		
		
		return exerciseList;
		
	}
	
	
	private XYMultipleSeriesRenderer getDemoRenderer2(ArrayList<Exercise> exerciseList) {
		ArrayList<String> mbottomLabels = new ArrayList<String>();
		for(Exercise exercise : exerciseList){
			int i = 1;
			for(Set s : exercise.getSetList()){
				if(i == 1){
					mbottomLabels.add(""+exercise.getDate().toString().substring(3, 16));
				}else{
					mbottomLabels.add("Set "+i);	
				}
				i++;
			}
		}
		// Creating XYSeriesRenderer to customize incomeSeries
		XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
		incomeRenderer.setColor(Color.CYAN);
		incomeRenderer.setPointStyle(PointStyle.CIRCLE);
		incomeRenderer.setFillPoints(true);
		incomeRenderer.setLineWidth(2);
		incomeRenderer.setDisplayChartValues(true);

		// Creating XYSeriesRenderer to customize expenseSeries
		XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
		expenseRenderer.setColor(Color.MAGENTA);
		expenseRenderer.setPointStyle(PointStyle.CIRCLE);
		expenseRenderer.setFillPoints(true);
		expenseRenderer.setLineWidth(2);
		expenseRenderer.setDisplayChartValues(true);
//		expenseRenderer.setStroke(BasicStroke.DOTTED);
		
		// Creating XYSeriesRenderer to customize expenseSeries
		XYSeriesRenderer averageRenderer = new XYSeriesRenderer();
		averageRenderer.setColor(Color.GREEN);
		averageRenderer.setPointStyle(PointStyle.CIRCLE);
		averageRenderer.setFillPoints(true);
		averageRenderer.setLineWidth(2);
		averageRenderer.setDisplayChartValues(true);
		averageRenderer.setStroke(BasicStroke.DOTTED);


		// Creating a XYMultipleSeriesRenderer to customize the whole chart
		XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
		multiRenderer.setXLabels(0);
		multiRenderer.setChartTitle("Exercise Statistic " + exerciseList.get(0).getExerciseName());
		multiRenderer.setXTitle("Year 2013");
		multiRenderer.setYTitle("Value");
		multiRenderer.setZoomButtonsVisible(true);
		multiRenderer.setXAxisMax(7);
		
		for (int i = 0; i < mbottomLabels.size(); i++) {
			multiRenderer.addXTextLabel(i + 1, mbottomLabels.get(i));
		}

		multiRenderer.addSeriesRenderer(incomeRenderer);
		multiRenderer.addSeriesRenderer(expenseRenderer);
		multiRenderer.addSeriesRenderer(averageRenderer);

		return multiRenderer;

	}

	private XYMultipleSeriesDataset getDemoDataset2(ArrayList<Exercise> exerciseList) {
		ArrayList<Integer> weightList = new ArrayList<Integer>();
		ArrayList<Integer> repetitionList = new ArrayList<Integer>();
		int i = 0;
		
		for(Exercise exercise : exerciseList){
			for(Set s : exercise.getSetList()){
				weightList.add((int) s.getValueWeight());
				repetitionList.add(s.getValueInt());
			}
		}

		// Creating a dataset to hold each series
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		// Creating an XYSeries for Income
		XYSeries weightSeries = new XYSeries("Repetitions");
		// Creating an XYSeries for Expense
		XYSeries repetitionSeries = new XYSeries("Weight in kg");
		XYSeries averageSeries = new XYSeries("Average");
		// Adding data to Income and Expense Serie
		for (i = 0; i < weightList.size(); i++) {
			weightSeries.add((i+1), weightList.get(i));
			repetitionSeries.add((i+1), repetitionList.get(i));
			averageSeries.add((i+1), ((weightList.get(i) + repetitionList.get(i))/2));
		}

		// Adding Income Series to the dataset
		dataset.addSeries(weightSeries);
		// Adding Expense Series to dataset
		dataset.addSeries(repetitionSeries);
		
		dataset.addSeries(averageSeries);
		return dataset;
	}
}